# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=sd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.0.0
pkgrel=1
pkgdesc="Intuitive find & replace CLI (sed alternative) (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/chmln/sd'
msys2_references=(
  'archlinux: sd'
  'purl: pkg:cargo/sd'
)
license=('spdx:MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust")
options=('!strip') # it's done at build stage
source=("https://github.com/chmln/sd/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('2adc1dec0d2c63cbffa94204b212926f2735a59753494fca72c3cfe4001d472f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo build --release --frozen
}

check() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo test --release --frozen
}

package() {
  cd "${srcdir}/${_realname}-${pkgver}"

  cargo install \
    --offline \
    --no-track \
    --frozen \
    --path . \
    --root "${pkgdir}${MINGW_PREFIX}"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 gen/sd.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/sd.1"
  install -Dm644 gen/completions/sd.bash "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/sd"
  install -Dm644 gen/completions/_sd "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_sd"
  install -Dm644 gen/completions/sd.fish "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/sd.fish"
}
